package com.hdax.service.Impl;

import com.hdax.entity.Permission;
import com.hdax.mappers.PermissionMapper;
import com.hdax.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 许可服务impl
 *
 * @author Aizen
 * @date 2022/05/04
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    /**
     * 许可映射器
     */
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 根据角色ID拿到对应的所有权限
     *
     * @param rid 掉
     * @return {@link List}<{@link Permission}>
     */
    @Override
    public List<Permission> allPermissions(Integer rid) {
        return permissionMapper.allPermissions(rid);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,propagation = Propagation.REQUIRED)
    public void changePermission(Integer rid, List<Integer> ids) {
        //删除
        permissionMapper.delPermissionByRid(rid);
        //添加
        permissionMapper.addPermissionsByRid(rid, ids);
    }
}
